package common;

//import org.springframework.web.util.CookieGenerator;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;

public final class CookieUtils {
/*
	private static final CookieGenerator cook;

	static {
		cook = new CookieGenerator();
		cook.setCookieMaxAge(-1);
		cook.setCookieSecure(false);
		// cook.setCookieDomain( "/" );
		// cook.setCookiePath( "/" );
	}
*/
	// 생성자를 가질수 없음.
	private CookieUtils() {
		throw new UnsupportedOperationException();
	}

	public static String getValue(Cookie[] cookies, String name) {
		if (cookies == null) {
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			if (name.equals(cookies[i].getName())) {
				return cookies[i].getValue();
			}
		}
		return null;
	}

	/**
	 * @param cookies
	 * @param name
	 * @param decode
	 * @return
	 */
	public static String getDecodingValue(Cookie[] cookies, String name,
			String decode) {
		if (cookies == null) {
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			if (name.equals(cookies[i].getName())) {
				try {
					return java.net.URLDecoder.decode(cookies[i].getValue(),
							decode);
				} catch (UnsupportedEncodingException e) {
					return null;
				}
			}
		}
		return null;
	}

	/**
	 * @param response
	 * @param cook
	 * @param name
	 * @param value
	 */
	public static void setValue(HttpServletResponse response, String name,
			String value) {
		Cookie imsicookie = new Cookie(name, value);
		imsicookie.setMaxAge(-1);
		response.addCookie(imsicookie);
	}

	/**
	 * @param response
	 * @param cook
	 * @param name
	 * @param value
	 * @param encode
	 */
	public static void setEncodingValue(HttpServletResponse response,
			String name, String value, String encode) {

		String val = null;
		try {
			val = java.net.URLEncoder.encode(value, encode);
		} catch (UnsupportedEncodingException e) {
			// skip...
		}
		setValue(response,name,val);
	}

	/**
	 * 가장 마지막에 저장된 쿠키를 제거
	 * 
	 * @param response
	 */
	public static void removeCookie(HttpServletResponse response, HttpServletRequest request) {
		try {
			Cookie[] cookies = request.getCookies();
			if (cookies !=null && cookies.length>0){
				Cookie cookie = new Cookie(cookies[cookies.length].getName(), "");
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}

		} catch (Exception e) {
			System.err.println("remove cookie:" + e.getMessage());
		}
	}

	/**
	 * @param response
	 * @param request
	 * @param name
	 *            제거할 쿠키명
	 */
	public static void removeCookie(HttpServletResponse response,
			HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			try {
				if (name.equals(cookies[i].getName())) {
					Cookie cookie = new Cookie(name, "");
					cookie.setMaxAge(0);
					response.addCookie(cookie); // 쿠키를 덮어씀으로써 제거가 된다.
				}
			} catch (RuntimeException e) {
				// skip....
			}
		}

	}
}
